// 二分查找
// 矩阵可以视为有序数组
var searchMatrix = function (matrix, target) {
  let m = matrix.length;
  if (m == 0) return false;
  let n = matrix[0].length;
  let low = 0;
  let high = m * n - 1;
  while (low <= high) {
    let mid = (low + high) >> 1;
    let row = parseInt(mid / n);
    let col = mid % n;
    let matrixMid = matrix[row][col];
    if (matrixMid > target) {
      high = mid - 1;
    } else if (matrixMid < target) {
      low = mid + 1;
    } else if (matrixMid == target) {
      return true;
    }
  }
  return false;
};

var searchMatrix = function (matrix, target) {
  let arr = matrix.flat(),
    height = arr.length - 1,
    low = 0,
    mid;
  while (low <= height) {
    mid = Math.floor((low + height) / 2);
    if (arr[mid] == target) {
      return true;
    } else if (arr[mid] < target) {
      low = mid + 1;
    } else {
      height = mid - 1;
    }
  }
  return false;
};
